package com.example.yyy

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_view_pager_f.*

class ViewPagerFActivity : AppCompatActivity() {

    private var bgChangReceiver : BgChangReceiver? = null
    //这条语句我们要放在全局变量来读取，当你改变值或者值被改变时，这里的值会自己提交和同步
    private var name:String by Preference(this,"name","沙雕")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager_f)
        val goods1 = Goods(R.drawable.tp, 20, "这是一个图片", "标题一")
        val goods2 = Goods(R.drawable.tp, 30, "这就是一个图片", "标题二")
        val goods3 = Goods(R.drawable.tp, 40, "这还是一个图片", "标题三")
        val goods4 = Goods(R.drawable.tp, 50, name, "标题四")
        val goodList = mutableListOf<Goods>()
        goodList.add(goods1)
        goodList.add(goods2)
        goodList.add(goods3)
        goodList.add(goods4)
        vp_vpf_content.adapter = mPagerAdapter(supportFragmentManager,goodList)
        vp_vpf_content.currentItem = 0
        tab_vpf_title.setupWithViewPager(vp_vpf_content)
    }

    override fun onStart() {
        super.onStart()
        bgChangReceiver = BgChangReceiver()
        //过滤器
        val filter = IntentFilter(DyFragment.EVENT)
        //注册广播
        registerReceiver(bgChangReceiver,filter)
    }

    override fun onStop() {
        super.onStop()
        //注销广播
        unregisterReceiver(bgChangReceiver)
        name = "$name 1"
    }
    private inner class BgChangReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null){
                val color = intent.getStringExtra("color")
                ll_vpf_color.setBackgroundColor(Color.parseColor(color))
            }
        }
    }



}
